/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.wiredsys.credcheck.dao.util;

import java.io.Serializable;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;

/**
 *
 * @author ppacheco
 */
@SuppressWarnings("serial")
public class GenericDAOService implements Serializable {

    /**
     *
     */
    private ServiceRegistry serviceRegistry = null;
    private SessionFactory sessionFactory = null;
    private static GenericDAOService instance = null;
    private static final Logger log = Logger.getLogger(GenericDAOService.class
            .getName());

    /**
     *
     */
    public GenericDAOService() {
        log.log(Level.INFO, "Loading GenericDAOSerivce.");
    }

    private ServiceRegistry getServiceRegistry() throws HibernateException {

        try {
            Configuration configuration = new Configuration();
            configuration.configure();
            serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
        } catch (HibernateException e) {
            log.log(Level.ERROR, e.getMessage(), e);
        }
        return serviceRegistry;
    }

    /**
     *
     * @return @throws HibernateException
     */
    private SessionFactory getSessionFactory() throws HibernateException {

        try {
            Configuration config = new Configuration();
            config.configure();
            sessionFactory = config.buildSessionFactory(getServiceRegistry());
        } catch (HibernateException e) {
            log.log(Level.ERROR, e.getMessage(), e);
        }
        return sessionFactory;
    }

    /**
     *
     * @param session
     * @return
     * @throws HibernateException
     */
    public Session getSession(Session session) throws HibernateException {

        try {
            if (session == null) {
                log.log(Level.INFO, "Opening new org.hibernate.Session");
                session = getSessionFactory().openSession();
                log.log(Level.INFO, "org.hibernate.Session >>> Connected = " + session.isConnected() + " Opened = " + session.isOpen());
            }
        } catch (HibernateException e) {
            log.log(Level.ERROR, e.getMessage(), e);
        }
        return session;
    }

    /**
     *
     * @param session
     * @throws HibernateException
     */
    public void closeSession(Session session) throws HibernateException {

        try {
            if (session != null) {
                log.log(Level.INFO, "Flushing org.hibernate.Session");
                session.flush();
                log.log(Level.INFO, "Closing org.hibernate.Session");
                session.close();
            }
        } catch (HibernateException e) {
            log.log(Level.ERROR, e.getMessage(), e);
        }
    }

    /**
     *
     * @return @throws InstantiationException
     */
    public static GenericDAOService getInstance() throws InstantiationException {

        try {
            if (instance == null) {
                instance = new GenericDAOService();
            }
        } catch (Exception e) {
            log.log(Level.ERROR, e.getMessage(), e);
        }
        return instance;
    }
}
